package com.wondersgroup.retrofittest.mall;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.aspsine.swipetoloadlayout.listener.RecyclerViewScrollerListener;
import com.hss.mvp.base.BaseMVPFragment;
import com.hss.rxjava.bean.GoodsBean;
import com.meetme.android.multistateview.MultiStateView;
import com.wondersgroup.retrofittest.R;
import com.wondersgroup.retrofittest.mall.contract.MallContract;

import java.util.LinkedList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/2/10.
 */

public class MallFragment extends BaseMVPFragment<MallContract.Presenter> implements MallContract.View {


    @Bind(R.id.swipe_target)
    RecyclerView swipeTarget;
    @Bind(R.id.swipe_layout)
    SwipeToLoadLayout swipeLayout;
    @Bind(R.id.msv_layout)
    MultiStateView msvLayout;



   int  isDrop = 0;
    int pageNo = 1;
    RecyclerGoodsAdapter goodsAdapter = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.msv_refresh_recyclerview, container, false);
        ButterKnife.bind(this, root);
        return root;
    }

    @Override
    public void showMSVLoading() {
        msvLayout.setState(MultiStateView.ContentState.LOADING);
    }

    @Override
    public void showMSVEmpty() {
        msvLayout.setState(MultiStateView.ContentState.EMPTY);

    }

    @Override
    public void showMSVNetErr() {
        msvLayout.setState(MultiStateView.ContentState.ERROR_NETWORK);

    }

    @Override
    public void showMSVUnknowErr() {
        msvLayout.setState(MultiStateView.ContentState.ERROR_GENERAL);

    }

    @Override
    public void showMSVContent() {
        msvLayout.setState(MultiStateView.ContentState.CONTENT);

    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        swipeLayout.setOnLoadMoreListener(loadMoreListener);
        swipeLayout.setOnRefreshListener(refreshListener);
        swipeTarget.setLayoutManager(new LinearLayoutManager(getActivity()));
        swipeTarget.addOnScrollListener(new RecyclerViewScrollerListener(swipeLayout));
        if (null == goodsAdapter){
            goodsAdapter = new RecyclerGoodsAdapter();
        }
        swipeTarget.setAdapter(goodsAdapter);

    }

    @Override
    public void onResume() {
        super.onResume();
        if (goodsAdapter.getItemCount() == 0){
            showMSVLoading();
            swipeLayout.setLoadMoreEnabled(false);
            isDrop = 0;
            presenter.loadGoods(isDrop,pageNo,10);

        }else {
            showMSVContent();
        }
    }

    OnLoadMoreListener loadMoreListener=  new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {

            loadMore();
        }
    };

    OnRefreshListener refreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            refresh();
        }
    };
    @Override
    public void refresh() {
        isDrop = 1;
        pageNo = 1;
        presenter.loadGoods(isDrop,pageNo,10);
    }

    @Override
    public void loadMore() {
        pageNo ++ ;
        isDrop= 2;
        presenter.loadGoods(isDrop,pageNo,10);
    }

    @Override
    public void refreshComplete() {
        swipeLayout.setRefreshing(false);
    }

    @Override
    public void loadMoreComplete() {
        swipeLayout.setLoadingMore(false);
    }

    @Override
    public void loadMoreEnable(boolean enable) {
        swipeLayout.setLoadMoreEnabled(enable);
    }

    @Override
    public <GoodsBean> void refreshData(boolean isRefresh, List<GoodsBean> list) {
        if (isRefresh){
            goodsAdapter.setData((List<com.hss.rxjava.bean.GoodsBean>) list);
        }else {
            goodsAdapter.addList((List<com.hss.rxjava.bean.GoodsBean>) list);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
